package com.cortex.pineapple.listener;

import com.cortex.pineapple.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;

public class DragDropView {

	private ImageView dragCopyView;
	private int[] locationDragView = new int[2];
	private Context context;
	private View dragView;
	private OnStateChangedListener onStateChangedListener;

	public DragDropView(){
		
	}
	public DragDropView(View v){
		setDragView(v);
	}
	public void setDragView(View dragView) {
		this.dragView = dragView;
		context = dragView.getContext();
		init();
	}
	private void init(){
		if(dragView == null){
			return;
		}
		dragView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent e) {
				int x = (int) e.getX();
				int y = (int) e.getY();

				switch(e.getAction()){
				case MotionEvent.ACTION_DOWN:					
					dragView.getLocationInWindow(locationDragView);
					startDrag(dragView,locationDragView[0], locationDragView[1]);		
					return true;
				case MotionEvent.ACTION_MOVE:
					drag(locationDragView[0] + x, locationDragView[1] + y);
					break;
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_CANCEL:
					stopDrag(locationDragView[0] + x, locationDragView[1] + y);
					break;
				}
				return true;
			}
		});
	}
	public void setOnStateChangedListener(OnStateChangedListener onStateChangedListener) {
		this.onStateChangedListener = onStateChangedListener;
	}
	// enable the drag view for dragging
	private void startDrag(View v,int x, int y) 
	{
		stopDrag(x,y);

		if (v == null) return;
		v.setDrawingCacheEnabled(true);
		// Create a copy of the drawing cache so that it does not get recycled
		// by the framework when the list tries to clean up memory
		Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());

		WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
		windowParams.gravity = Gravity.TOP | Gravity.LEFT;
		windowParams.x = x;
		windowParams.y = y;


		windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
		windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
		windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
				| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
		windowParams.format = PixelFormat.TRANSLUCENT;
		windowParams.windowAnimations = 0;

		ImageView copy = new ImageView(context);
		copy.setImageBitmap(bitmap);      

		WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
		windowManager.addView(copy, windowParams);
		copy.setBackgroundColor(context.getResources().getColor(R.color.white));
		dragCopyView = copy;
		if(onStateChangedListener != null){
			onStateChangedListener.startDragging(dragView,x,y);
		}
			
	}
	// move the drag view
	private void drag(int x, int y) 
	{
		if (dragCopyView != null) 
		{
			WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) dragCopyView.getLayoutParams();
			layoutParams.x = x;
			layoutParams.y = y;
			WindowManager mWindowManager = (WindowManager) context
					.getSystemService(Context.WINDOW_SERVICE);
			mWindowManager.updateViewLayout(dragCopyView, layoutParams);

		}
		if(onStateChangedListener != null){
			onStateChangedListener.moveDragging(dragView,x,y);
		}
	}
	// destroy drag view
	private void stopDrag(int x,int y) 
	{
		if (dragCopyView != null) {
			dragCopyView.setVisibility(View.GONE);
			WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
			wm.removeView(dragCopyView);
			dragCopyView.setImageDrawable(null);
			dragCopyView = null;
			if(onStateChangedListener != null){
				onStateChangedListener.stopDragging(dragView,x,y);
			}
		}
	}   

	public interface OnStateChangedListener{
		void startDragging(View dragView,int x,int y);
		void moveDragging(View dragView,int x,int y);
		void stopDragging(View dragView,int x,int y);
	}
}
